package com.lanou.controller;

import com.lanou.bean.RoleInfo;
import com.lanou.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by lanouhn on 17/10/18.
 */
@Controller
public class RoleController {

    ModelAndView mav = new ModelAndView();

    @Resource
    private RoleService roleService;

    @RequestMapping("/selectAll")
    public ModelAndView selectAll() {
        List<RoleInfo> roleInfos = roleService.selectAll();
        mav.addObject("AllRoleAndModule", roleInfos);
        mav.setViewName("/role/role_list");
        return mav;
    }


    @RequestMapping("/selectByName")
    public ModelAndView selectByName(String role_name, HttpSession session, String module) {
//        从前端传过来的数据
        System.out.println(module);
        System.out.println(role_name);
//        取数据库中查看是否有这个name
        RoleInfo roleInfo = roleService.selectByName(role_name);

        if (roleInfo != null) {
//            代表存在，不能插入
            session.setAttribute("msg", "用户名已存在");
        } else {
//            插入数据
//        向role_info插入数据
            roleService.insertRoleAndModuleByName(role_name);
//        获取插入数据的id
            Integer role_id = roleService.selectIdByName(role_name);
            System.out.println(role_id);

//        将前端的module截取,获取对应的module中id
            String[] modulelist = module.split(",");

//        创建一个数组，存储权限的id
            int[] moduleid = new int[modulelist.length];
//            遍历数组
            for (int i = 0; i < modulelist.length; i++) {
                System.out.println(modulelist[i]);
                moduleid[i] = roleService.selectIdToModule(modulelist[i]);
                System.out.println(moduleid[i]);
            }
//         roel_module表插入关联
            System.out.println("获取到的权限个数:" + moduleid.length);
            for (int i = 0; i < moduleid.length; i++) {
                roleService.insertRoleModuleById(role_id, moduleid[i]);
            }
//            不存在，可以插入
            session.setAttribute("msg", "保存成功");
        }
        mav.setViewName("/role/role_add");
        return mav;
    }


//    根据id查询回显
    @RequestMapping("/selectByRoleId")
    public ModelAndView selectByRoleId(Integer rId, HttpSession session) {
        System.out.println(rId);
        RoleInfo roleInfo = roleService.selectByRoleId(rId);

        mav.addObject("roleInfo", roleInfo);
        mav.setViewName("/role/role_modi");
        return mav;

    }

    @RequestMapping("/deleteRole")
    public ModelAndView deleteRole(Integer roleId) {
        System.out.println(roleId);

        roleService.deleteRoleAndModuleById(roleId);

        roleService.deleteAdminRoel(roleId);

        roleService.deleteByPrimaryKey(roleId);
        return mav;
    }


    //    修改后保存
    @RequestMapping("/update")
    public ModelAndView update(String roleName,Integer roleId,String inputname,String module,HttpSession session) throws UnsupportedEncodingException {


        System.out.println(roleId+roleName+inputname+module);

//        删除role数据(根据id删除role_info数据)
//        role_info更新数据

        int result=roleService.updateRoelInfoById(roleId,inputname);
        System.out.println(result+"更新成功");
//      删除中间表关联
        int result1=roleService.delectRoleModule(roleId);
        System.out.println(result1+"删除中间表关联成功");

//        将前端的module截取,获取对应的module中id
            String[] modulelist = module.split(",");

//        创建一个数组，存储权限的id
            int[] moduleid = new int[modulelist.length];
//            遍历数组
            for (int i = 0; i < modulelist.length; i++) {
                System.out.println(modulelist[i]);
                moduleid[i] = roleService.selectIdToModule(modulelist[i]);
                System.out.println(moduleid[i]);
            }
//         roel_module表插入关联
            System.out.println("获取到的权限个数:" + moduleid.length);
            for (int i = 0; i < moduleid.length; i++) {
                roleService.insertRoleModuleById(roleId, moduleid[i]);
            }

        mav.setViewName("role/role_list");
        return mav;
    }
}
